# coding: utf-8

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import mplfinance as mpf

pd_data = pd.read_excel('../399330_weekly.xlsx')
pd_data = pd_data[800:]

pd_data['MA30'] = pd_data['收盘'].rolling(window=30).mean()
ewma12 = pd_data['收盘'].ewm(span=12, adjust=False).mean()
ewma26 = pd_data['收盘'].ewm(span=26, adjust=False).mean()
pd_data['dif'] = ewma12 - ewma26
pd_data['dea'] = pd_data['dif'].ewm(span=9, adjust=False).mean()
pd_data['bar'] = (pd_data['dif'] - pd_data['dea']) * 2


# fig = plt.figure(figsize=(24, 15))
# axs0 = fig.add_axes([0.1, 0.5, 0.8, 0.35])
# axs1 = fig.add_axes([0.1, 0.1, 0.8, 0.35])
# axs = [axs0, axs1]
# candlestick2_ochl(ax=axs[0], opens=pd_data['开盘'], closes=pd_data['收盘'], highs=pd_data['最高'],lows=pd_data['最低'], width=0.5, colorup='r', colordown='g')
# axs[0].plot(np.arange(0, len(pd_data.index) - 0), pd_data['MA30'], 'blue', label='MA30', lw=1)
#



pd_data['Date'] = pd.to_datetime(pd_data['日期'])
pd_data.set_index('Date', inplace=True)
# 重命名列名为英文（若数据列是中文名）
pd_data = pd_data.rename(columns={
    '开盘': 'Open',
    '收盘': 'Close',
    '最高': 'High',
    '最低': 'Low',
    '成交量': 'volume'
})

# 设置自定义样式（颜色、K线宽度）
style = mpf.make_mpf_style(
    marketcolors=mpf.make_marketcolors(
        up='r',     # 阳线颜色（收盘价 >= 开盘价）
        down='g',   # 阴线颜色
        # wick='black', # 影线颜色
        # up='#FF0000',   # 阳线颜色（橙红）
        # down='#00FF00', # 阴线颜色（亮绿）
        wick='black',   # 影线颜色
        alpha=1.0       # 不透明度
    ),
    base_mpf_style='binance',
    y_on_right=False,  # 将y轴刻度放在左侧
    gridaxis='both'    # 显示网格线
)
apds = [mpf.make_addplot(pd_data['Close'].rolling(window=20).mean(), panel=0, color='g')]
mpf.plot(
    pd_data,
    type='candle',
    # ax=axs0,
    style=style,
    datetime_format='%Y-%m-%d',  # 日期格式
    xrotation=45,                # x轴标签旋转角度
    tight_layout=True,        # 紧凑布局
    mav=(10, 30),
    addplot=apds,
)


# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False
# plt.show()